import json
from pyecharts.charts import Line
import pyecharts.options as opts

# 处理数据
f_us = open("D:/美国.txt", "r", encoding="UTF-8")
us_data = f_us.read()

f_jp = open("D:/日本.txt", "r", encoding="UTF-8")
jp_data = f_jp.read()

f_in = open("D:/印度.txt", "r", encoding="UTF-8")
in_data = f_in.read()

# 去掉开头
us_data = us_data.replace("jsonp_1629344292311_69436(", "")
jp_data = jp_data.replace("jsonp_1629350871167_29498(", "")
in_data = in_data.replace("jsonp_1629350745930_63180(", "")

# 去掉结尾
us_data = us_data[:-2]
jp_data = jp_data[:-2]
in_data = in_data[:-2]

# json转python字典
us_dict = json.loads(us_data)
jp_dict = json.loads(jp_data)
in_dict = json.loads(in_data)
# print(type(us_dict))
# print(us_dict)

# 获取 trend key
us_trend_data = us_dict['data'][0]['trend']
jp_trend_data = jp_dict['data'][0]['trend']
in_trend_data = in_dict['data'][0]['trend']
# print(trend_data)

# 获取X轴数据
x_data = us_trend_data['updateDate'][:314]

# 获取Y轴数据
us_y_data = us_trend_data['list'][0]['data'][:314]
jp_y_data = jp_trend_data['list'][0]['data'][:314]
in_y_data = in_trend_data['list'][0]['data'][:314]

# 生成图表
line = Line()  # 构建折线图对象
# 添加x轴数据
line.add_xaxis(x_data)
# 添加y轴数据
line.add_yaxis("美国确诊人数", us_y_data, label_opts=opts.LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数", jp_y_data, label_opts=opts.LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数", in_y_data, label_opts=opts.LabelOpts(is_show=False))

# 设置全局选项
line.set_global_opts(
    title_opts=opts.TitleOpts(title="2020年美日印三国确诊人数对比折线图", pos_left='center', pos_bottom='1%')
)

# 调用render()方法，生成图表
line.render()

# 关闭文件对象
f_us.close()
f_jp.close()
f_in.close()
